/* import config from '@/settings' */
import {
	documentFlow,
	aDownFile
} from './downFile'
import {
	uploadFilePromise
} from './upload'



/**
 * 打开链接
 * @param {String} url
 */
export const openLink = (url) => {
	window.open(url)
}

/**
 * 图片预览
 * @param {Array} urls -图片地址列表
 * @param {Number} current -当前显示的图片链接，建议使用png、jpg格式图片
 */
export const previewImage = (urls, current) => {
	uni.previewImage({
		urls,
		current, // 当前图片链接或当前图片下标
		indicator: 'default',
		loop: true
	})
}

/**
 * 设置导航栏标题
 * @param {String} title
 */
export const setTitle = (title) => {
	if (isInDingTalk()) {
		dd.ready(function() {
			dd.biz.navigation.setTitle({
				title
			});
		})
	}
}

/**
 * showLoading
 */
export const showLoading = (text = 'Loading') => {
	uni.showLoading({
		title: text
	})
}


/**
 * hideLoading
 */
export const hideLoading = () => {
	uni.hideLoading()
}


/**
 * 数据合并  (字典)
 */
export const mergeRecursive = (source, target) => {
	for (let p in target) {
		try {
			if (target[p].constructor == Object) {
				source[p] = mergeRecursive(source[p], target[p]);
			} else {
				source[p] = target[p];
			}
		} catch (e) {
			source[p] = target[p];
		}
	}
	return source;
};



/**
 * 数组交集
 * @param { Array } arr1
 * @param { Array } arr2
 */
export const intersection = (arr1, arr2) => {
	let a = new Set(arr1),
		b = new Set(arr2);
	return [...a].filter(v => [...b].includes(v))
}


/**
 * 定时器
 */
export const setTimeOutFun = (_this, type) => {
	_this.time = setTimeout(() => {
		/*if (!_this.isTime) {
		    clearTimeout(_this.time)
		    return
		}*/
		// 当前时间
		_this.nowTime = formatTime(new Date(), type)
		setTimeOutFun(_this, type)
	}, 1000)
}


/**
 * 是否pc
 */
export const isMobile = () => {
	let flag = navigator.userAgent.match(
		/(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone)/i
	)
	return flag ? true : false;
}


/**
 * 验证是否是图片
 * @param {String} filePath -图片地址
 */
export const isAssetTypeAnImage = (filePath) => {
	let imgArr = ['png', 'jpg', 'jpeg', 'bmp', 'gif', 'webp', 'psd', 'svg', 'tiff']
	let fileType = filePath.slice(filePath.lastIndexOf(".") + 1).toLowerCase()
	return imgArr.includes(fileType)
}

/**
 * 获取文件名
 * @param {String} file -地址
 */
export const getFileName = (file) => {
	return file.slice(file.lastIndexOf("/") + 1)
}


/**
 * toast
 * @param {String} title
 * @param {String} icon
 */
export const toast = (title, icon) => {
	//兼容性问题 系统、钉钉版本有关
	// if (dd.env.platform !== "notInDingTalk") {
	// 	dd.ready(function() {
	// 		dd.device.notification.toast({
	// 			icon: '',
	// 			text: title,
	// 			duration: 2
	// 		})
	// 	})
	// } else {
	uni.showToast({
		title,
		icon: icon || 'none',
		duration: 2000
	})
	// }
}

/**
 * 获取url参数
 * @param {String} name -参数名
 */
export const getQueryString = (name) => {
	// /corpId=([^=&]*)/.exec(window.location.href)[1]
	const reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i');
	const search = window.location.search.split('?')[1] || '';
	const r = search.match(reg) || [];
	return r[2];
}
/**
 * 将字符串拆分成数组
 * @param {String} str -字符串
 * @param {String} split -拆分的字符
 */
export const splitStrToArray = (str, split=',') => {
	return str?str.split(split):[]
}

/*
 *判断文件类型
 */
export const fileFormat =  function isImageFile(arr,filePath){
	//获取最后一个.的位置
	let index= filePath.lastIndexOf(".");
	//获取后缀
	let ext = filePath.substring(index+1);
	return arr.indexOf(ext.toLowerCase()) != -1?true:false

}
export const generateId = () => {
	const str = [];
	const hexDigits = "0123456789abcdef";
	for (let i = 0; i < 36; i++) {
		str[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
	}
	str[14] = "4";
	str[19] = hexDigits.substr((str[19] & 0x3) | 0x8, 1);
	return str.join("");
};

export function daysUntil(targetDateStr) {
	// 当前时间
	const now = new Date();

	// 目标时间（解析日期字符串为 Date 对象）
	const targetDate = new Date(targetDateStr);

	// 确保日期只计算天（清除时间部分影响）
	targetDate.setHours(0, 0, 0, 0);
	now.setHours(0, 0, 0, 0);

	// 计算时间差（毫秒）
	const diffInMilliseconds = targetDate.getTime() - now.getTime();

	// 转换为天
	const diffInDays = diffInMilliseconds / (1000 * 60 * 60 * 24);

	return Math.ceil(diffInDays); // 向上取整以覆盖当天剩余时间
}

export {
	documentFlow,
	uploadFilePromise,
	aDownFile,
}
